package com.tuteng.administrator.ashospital.Activity;

import android.os.Bundle;
/**
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;**/
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.tuteng.administrator.ashospital.AdapterManger.FriendAdapter;
import com.tuteng.administrator.ashospital.JsonCallback;
import com.tuteng.administrator.ashospital.R;
import com.tuteng.administrator.ashospital.application.hospitalAppication;
import com.tuteng.administrator.ashospital.beans.BindCardResponse;
import com.tuteng.administrator.ashospital.beans.DeleteCardRequest;
import com.tuteng.administrator.ashospital.beans.FriendInfoBean;
import com.tuteng.administrator.ashospital.comprehensive.LoadingDialogManager;

import java.util.List;

import static com.tuteng.administrator.ashospital.Config.GlobleConfig.APP_URL;

public class MannageFriendActivity extends  BaseActivity implements View.OnClickListener{
private TextView mPatientName;
    private ImageButton mImgback;
    private Button mDeleteBtn;
    private  TextView mTvbrxb;
    private  TextView mTvbrnl;
    private TextView mTvbrdh;
    private  TextView mTvsfzh;
    private TextView mTvbrjtzz;
    private hospitalAppication app;
    private  TextView mAddFriend;
    private ListView mList;
    private LinearLayout mMxlin;
    private LinearLayout mListlin;
    private RelativeLayout mAddRe;
    private FriendAdapter friendAdapter;
    @Override
    protected void initEnvironment() {
     app=hospitalAppication.getInstance();
    }

    @Override
    protected void initViews() {
        mPatientName=(TextView)findViewById(R.id.myfriend_tv_brxm);
        mTvbrxb=(TextView)findViewById(R.id.myfriend_tv_ghks);
        mTvbrnl=(TextView)findViewById(R.id.mzbl_tv_jzys);
        mTvbrdh=(TextView)findViewById(R.id.mzbl_tv_jzsj);
        mTvsfzh=(TextView)findViewById(R.id.mzbl_tv_sfzh);
        mTvbrjtzz=(TextView)findViewById(R.id.mzbl_tv_brjtzj);
        mImgback=findViewById(R.id.img_uc_center_back);
        mImgback.setOnClickListener(this);
        mDeleteBtn= findViewById(R.id.login_button_cancle);
     //   mDeleteBtn.setOnClickListener(this);
        mList=findViewById(R.id.list_myfriend);
        mMxlin=findViewById(R.id.myfriend_ll_delete);
        mListlin=findViewById(R.id.ll_myfriend_list);
        mAddRe=findViewById(R.id.re_add_friend);
        final BindCardResponse response=app.getResponse();
        if(response.getFriendsInfo()==null)
        {mListlin.setVisibility(View.GONE);
        mMxlin.setVisibility(View.GONE);
        mAddRe.setVisibility(View.VISIBLE);
        }
        else {
            mMxlin.setVisibility(View.GONE);
            mAddRe.setVisibility(View.GONE);
            mListlin.setVisibility(View.VISIBLE);
            final List<FriendInfoBean> infoBeans=response.getFriendsInfo();
            friendAdapter=new FriendAdapter(MannageFriendActivity.this,infoBeans);
            mList.setAdapter(friendAdapter);
            mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                    mMxlin.setVisibility(View.VISIBLE);
                    mAddRe.setVisibility(View.GONE);
                    mListlin.setVisibility(View.GONE);
                    mPatientName.setText(infoBeans.get(position).getPatientName());
                    mTvbrxb.setText(infoBeans.get(position).getSex());
                    mTvbrnl.setText(infoBeans.get(position).getAge());
                    mTvbrdh.setText(infoBeans.get(position).getPhone());
                    mTvsfzh.setText(infoBeans.get(position).getCardNo());
                    mTvbrjtzz.setText(infoBeans.get(position).getAddress());
                    mDeleteBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            DeleteCardRequest request=new DeleteCardRequest();
                            request.setUid(response.getUserInfo().getUid());
                            request.setCardNo(infoBeans.get(position).getCardNo());
                            String js= JSON.toJSONString(request);
                            String url=APP_URL+"/DeleteCard";
                            OkGo.<BindCardResponse>post(url).upJson(js).execute(new JsonCallback<BindCardResponse>() {
                                @Override
                                public void onStart(Request<BindCardResponse, ? extends Request> request) {
                                    LoadingDialogManager.getInstance().showDialog();
                                }

                                @Override
                                public void onFinish() {
                                    LoadingDialogManager.getInstance().dismissDialog();
                                }

                                @Override
                                public void onSuccess(Response<BindCardResponse> response) {
                                    BindCardResponse r=response.body();
                                    app.setResponse(r);
                                    Toast.makeText(MannageFriendActivity.this,r.getResultMsg(),Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            });
                        }
                    });
                }
            });

        }
    }

    @Override
    protected int getLayoutId() {
        return (R.layout.activity_my_friend_mx);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        { case  R.id.img_uc_center_back:
            finish();
            break;
//            case  R.id.login_button_cancle:
//
//                break;
            default: break;

        }
    }
}
